package com.zqq.base.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.zqq.base.R;
import com.zqq.loading.AVLoadingIndicatorView;


/**
 * description:默认: 按空白处不能取消动画 ;“返回键”不取消
 * project name: ProjectMaster
 * author: Drink1993
 * version 1.0
 * create at:  2017/8/15 11:34
 * change at:
 * change describe:
 * change version:
 */

public class HttpDialog extends Dialog {
    private Context context;
    private ImageView iv_loading;
    private TranslateAnimation translateAnimation;
    private AnimationDrawable animationDrawable;
    private AVLoadingIndicatorView av_loading;
    private TextView tv_dialog;
    private String showMsg = "请求中";
    private int num = 0;

    private Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            if (num >= 4) {
                num = 0;
            }
            String show = "   ";
            if (num == 1) {
                show = ".  ";
            } else if (num == 2) {
                show = ".. ";
            } else if (num == 3) {
                show = "...";
            }
            tv_dialog.setText(showMsg + show);
            num++;
            handler.removeMessages(1);
            handler.sendEmptyMessageDelayed(1, 500);
        }
    };

    public HttpDialog(Context context) {
        super(context, R.style.dialog_style_loading);
        this.context = context;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_loading);
        //按空白处不能取消动画
        setCanceledOnTouchOutside(false);
        // 不可以用“返回键”取消
        setCancelable(false);
        //初始化界面控件
        initView();
        //初始化界面数据
        initData();
        //初始化界面控件的事件
        initEvent();
    }

    private void initView() {
        av_loading = findViewById(R.id.av_loading);
        tv_dialog = findViewById(R.id.tv_dialog);
    }

    @Override
    public void show() {
        super.show();
        if (animationDrawable != null) {
            animationDrawable.start();
        }

        if (translateAnimation != null) {
            iv_loading.startAnimation(translateAnimation);
        }

        av_loading.show();

        handler.removeMessages(1);
        handler.sendEmptyMessageDelayed(1, 500);
    }

    public void setMsg(String msg) {
        this.showMsg = msg;
        tv_dialog.setText("请求中");
    }

    @Override
    public void dismiss() {
        super.dismiss();

        if (animationDrawable != null) {
            animationDrawable.stop();
        }

        if (translateAnimation != null) {
            translateAnimation.cancel();
        }

        av_loading.hide();
    }

    private void initData() {

    }

    private void initEvent() {

    }
}
